<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 200px;
        height: 200px;
        background-color: red;
      }
    </style>
  </head>
  <body>
    <div class="box" id="box"></div>
    <script>
      //   box.onmousemove =

      function move(e) {
        console.log('mousemove触发了')
        console.log(e)
        console.log(this)
      }

      // 节流
      // 节流的原理: 让函数执行之间延迟一段时间(2s), 在计时的这段时间,如果再次触发这个函数,则函数不调用.什么时候时间到了,我们再次触发才让函数调用
      function throtte(func, time) {
        let timer = null
        return function (e) {
          if (timer) return
          timer = setTimeout(() => {
            func.apply(this, [e])
            timer = null
          }, time)
        }
      }

      box.onmousemove = throtte(move, 2000)
      //   box.onmousemove = move
    </script>
  </body>
</html>
